package com.zznode.redsun.commons.sercurity.action;

import java.util.Collection;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.zznode.csp.domain.commons.persistence.EntityManager;
import com.zznode.redsun.commons.sercurity.mapper.SecurityMapper;
import com.zznode.redsun.commons.sercurity.model.Permission;
import com.zznode.redsun.commons.sercurity.model.Role;
import com.zznode.redsun.commons.sercurity.service.SecurityService;
import com.zznode.redsun.commons.utils.AbstractBaseAction;

@Controller
@ParentPackage("json-default")
@Namespace("/system/role")
public class RoleAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	private SecurityService securityService;

	@Autowired
	private SecurityMapper securityMapper;

	@Autowired
	private EntityManager entityManager;

	private Role role;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp") })
	public String index() {
		return SUCCESS;
	}

	@Action(value = "listRoles", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_ROWS }) })
	public String listRoles() {
		Collection<Role> roles = securityMapper.findAllRoles();
		this.fillJsonGridRows(roles);
		return SUCCESS;
	}

	@Action(value = "addNew", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String addNew() {
		this.role = new Role();
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String edit() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.role = entityManager.get(Role.class, Long.valueOf(id));
		} else {
			this.role = new Role();
		}
		return SUCCESS;
	}

	@Action(value = "save", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String save() {
		if (null != this.role) {
			securityService.saveRole(role);
		} else {
			this.setJsonMessage("保存失败，提交数据为空！");
		}
		return SUCCESS;
	}

	@Action(value = "remove", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String remove() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			if (Role.ADMIN_ROLE_ID.equals(id)) {
				this.setMessage("不允许删除系统管理员的系统权限！");
			} else {
				entityManager.deleteById(Role.class, Long.valueOf(id));
			}
		} else {
			this.setJsonMessage("删除失败，提交数据为空!");
		}
		return SUCCESS;
	}

	@Action(value = "listPermissions", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_ROWS }) })
	public String listPermissions() {
		String roleId = this.getParameter("roleId");
		if (StringUtils.hasText(roleId)) {
			Collection<Permission> permissions = securityMapper.findRolePermissions(roleId);
			this.fillJsonGridRows(permissions);
		}
		return SUCCESS;
	}

	@Action(value = "addPermissions", results = { @Result(name = SUCCESS, location = "permissions.jsp") })
	public String addPermissions() {
		String roleId = this.getParameter("roleId");
		if (StringUtils.hasText(roleId)) {
			Collection<Permission> permissions = this.securityMapper.findChoosablePermissions(roleId);
			this.putValueStack("permissions", permissions);
		}
		this.putValueStack("roleId", roleId);
		return SUCCESS;
	}

	@Action(value = "savePermissions", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String savePermissions() {
		String roleId = this.getParameter("roleId");
		String[] permissionIds = (String[]) this.getValueStack("id");
		if (StringUtils.hasText(roleId) && ArrayUtils.isNotEmpty(permissionIds)) {
			this.securityService.addPermission(Long.valueOf(roleId), permissionIds);
		} else {
			this.setJsonMessage("数据保存失败，提交数据为空！");
		}
		return SUCCESS;
	}

	@Action(value = "removePermission", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String removePermission() {
		String roleId = this.getParameter("roleId");
		String permissionId = this.getParameter("permissionId");
		if (StringUtils.hasText(roleId) && StringUtils.hasText(permissionId)) {
			this.securityService.removePermission(Long.valueOf(roleId), permissionId);
		} else {
			this.setJsonMessage("删除失败，提交数据为空!");
		}
		return SUCCESS;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

}
